﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FGrubert.CrmQueueMonitor.WinForms.InfoMonitor
{
    public partial class FrmInfo : Form
    {
        private WindowConfiguration _config;
        /// <summary>
        /// Gets the configuration of the window
        /// </summary>
        public WindowConfiguration Config
        {
            get { return _config; }
        }

        private WebBrowser _browser;

        /// <summary>
        /// The enumerator for the current list
        /// </summary>
        private IEnumerator<InfoItem> _enumerator;

        /// <summary>
        /// Create a new information form with the given list of information
        /// </summary>
        /// <param name="_config">The configuration to use</param>
        public FrmInfo(WindowConfiguration config)
        {
            this._config = config;

            InitializeComponent();

            //ThreadStart threadStart = new ThreadStart(CreateBrowser);
            //Thread thread = new Thread(threadStart);
            //thread.Priority = ThreadPriority.BelowNormal;
            //thread.SetApartmentState(ApartmentState.STA);
            //thread.Start();
            CreateBrowser();
        }

        /// <summary>
        /// Refresh the current screen with the next information
        /// </summary>
        private void refreshTimer_Tick(object sender, EventArgs e)
        {
            RefreshContent();
        }

        /// <summary>
        /// Create the browser control
        /// </summary>
        private void CreateBrowser()
        {
            _browser = new WebBrowser();
            _browser.Parent = this;
            _browser.Dock = DockStyle.Fill;
            _browser.AllowWebBrowserDrop = false;
            _browser.ScriptErrorsSuppressed = true;
        }

        /// <summary>
        /// 
        /// </summary>
        private void RefreshContent()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new ThreadStart(RefreshContent));
            }
            else
            {
                try
                {
                    if (_enumerator == null || !_enumerator.MoveNext())
                    {
                        _enumerator = _config.Infos.GetEnumerator();
                        _enumerator.MoveNext();
                    }
                }
                catch (InvalidOperationException)
                {
                    // This happens, if the collection was changed, probably because some was editing it. So just create a new enumerator
                    _enumerator = _config.Infos.GetEnumerator();
                    _enumerator.MoveNext();
                }

                InfoItem newInfo = _enumerator.Current;

                if (newInfo != null)
                {
                    _browser.Navigate(newInfo.WebsiteUrl);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmInfo_Load(object sender, EventArgs e)
        {
            this.Location = _config.LastPosition;
            this.Size = _config.LastSize;

            this.Text = _config.WindowTitle;

            refreshTimer.Interval = _config.RefreshInterval * 1000;

            refreshTimer.Start();
            refreshTimer_Tick(null, null);
        }
    }
}
